package com.faxsun.profile.core.service;

import java.util.List;

import javax.annotation.Resource;

import org.broadleafcommerce.common.util.TransactionUtils;
import org.broadleafcommerce.profile.core.dao.AddressDao;
import org.broadleafcommerce.profile.core.dao.CustomerDao;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.faxsun.profile.core.dao.FSReceiverDao;
import com.faxsun.profile.core.domain.FSReceiver;

@Service("fsReceiverService")
public class FSReceiverServiceImpl implements FSReceiverService {

	@Resource(name = "receiverDao")
	protected FSReceiverDao receiverDao;

	@Resource(name = "blAddressDao")
	protected AddressDao addressDao;

	@Resource(name = "blCustomerDao")
	protected CustomerDao customerDao;

	@Override
	@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER)
	public FSReceiver save(FSReceiver receiver) {
		return receiverDao.save(receiver);
	}

	@Override
	public FSReceiver readById(long id) {
		return receiverDao.readById(id);
	}

	@Override
	@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER)
	public FSReceiver create() {
		return receiverDao.create();
	}

	@Override
	@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER)
	public void delete(FSReceiver receiver) {
		receiverDao.delete(receiver);
	}

	@Override
	public List<FSReceiver> readByCustomer(Customer customer) {

		// TODO: double check whether it is need to check customer's attributes

		if (customer == null)
			return null;
		List<FSReceiver> receivers = receiverDao.readByCustomer(customer);
		if (receivers.size() == 0)
			return null;

		return receivers;
	}

	@Override
	public FSReceiver readByEntity(FSReceiver receiver) {
		if(receiver==null)
			return null;
		
		List<FSReceiver> receivers=receiverDao.readByEntity(receiver);
		if(receivers.size()==0)
			return null;
		return receivers.get(0);
	}

//	@Override
//	public FSReceiver readByAddress(Address address) {
//		// TODO Auto-generated method stub
//		return null;
//	}

}
